{{define "roles.auth"}}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>角色授权</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/statics/third-pkg/layui/css/layui.css">
  <link rel="stylesheet" href="/statics/admin/css/public.css" media="all">
  <style>
    body {
      background-color: #ffffff;
    }
    .layui-iconpicker-body.layui-iconpicker-body-page .hide {display: none;}
  </style>
</head>

<body>
<div class="layui-form layuimini-form" style="margin-top: 50px;">
  <input name="id" type="hidden" value="{{ .model.ID }}">
  <div class="layui-form-item">
    <label class="layui-form-label required">权限</label>
    <div class="layui-input-block">
      <div id="permission" class="xm-select"></div>
    </div>
  </div>

  <div class="layui-form-item" style="margin-top: 30px;">
    <div class="layui-input-block">
      <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
    </div>
  </div>
</div>
<!--</div>-->
<script src="/statics/third-pkg/layui/layui.js"></script>
<script src="/statics/admin/js/lay-config.js?v=2.0" charset="utf-8"></script>

<script>

  layui.use(['form','request','layer',"miniTab",'common',"xmSelect"], function() {
    var form = layui.form,
            layer = layui.layer,
            miniTab = layui.miniTab,
            common=layui.common,
            xmSelect=layui.xmSelect,
            request=layui.request;
    var demo1 = xmSelect.render({
      el: '#permission',
      name: 'permissions_id',
      layVerify: 'required',
      layVerType: 'msg',
      filterable: true,
      tree: {
        //是否显示树状结构
        show: true,
        //是否展示三角图标
        showFolderIcon: true,
        indent: 20,
        //默认展开节点的数组, 为 true 时, 展开所有节点
        expandedKeys: true,
        strict: true,
        //是否开启极简模式
        simple: true,
      },

      height: 'auto',
      theme: {
        color: '#0081ff',
      },
      data: [
      ],
    })

    request.get('admin/permissions/list-tree').then(function (response) {
      if (response.data.code == 0) {
        var lists=response.data.data.lists;
        var initValue=[];
        {{if .ids }}
        initValue="{{ .ids }}".split(",").map(function (item){
           return Number.parseInt(item);
         })
        {{end }}
        demo1.update({
          data: lists,
          initValue,
        autoRow: true,
      })
      } else {
        layer.msg(response.data.msg);
      }
    }).catch(function (error) {
      console.log(error)
      layer.msg('获取权限失败');
    })
    //监听提交
    form.on('submit(saveBtn)', function(data) {
      var value = demo1.getValue();
     var  permissions_id=value.map(function (item){
        return item.value;
      }).join(',');
      var subData={
        ...data.field,
        permissions_id,
        "id":Number.parseInt(data.field["id"])
      }
      request.post('admin/roles/auth',subData).then(function (response) {
        if(response.status==204){
          layer.msg('授权成功',{
            time:1000
          },function (){
            localStorage.setItem("roles",common.generateUUID())
            miniTab.deleteCurrentByIframe();
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);

            window.location.href="/admin/roles/list";
          })
        }else{
          layer.msg(response.data.msg);
        }
      }).catch(function (error) {
        console.log(error)
        layer.msg('提交失败');
      });

    });

  });
</script>
</body>

</html>
{{ end }}